<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <link href="../../../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link href="../../../jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css"
          rel="stylesheet"/>

    <script type="text/javascript" src="../../../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../../../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

</head>
<body>

<div style="position:  relative; left: 30px;">
    <h3>修改字典值</h3>
    <div style="position: relative; top: -40px; left: 70%;">
        <button id="btnEdit" type="button" class="btn btn-primary">更新</button>
        <button type="button" class="btn btn-default" onclick="window.history.back();">取消</button>
    </div>
    <hr style="position: relative; top: -40px;">
</div>
<form class="form-horizontal" role="form">

    <div class="form-group">
        <label for="edit-dicTypeCode" class="col-sm-2 control-label">字典类型编码</label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="edit-dicTypeCode" style="width: 200%;">
                <option></option>
                <option>性别</option>
                <option>机构类型</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label for="edit-dicValue" class="col-sm-2 control-label">字典值<span style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="edit-dicValue" style="width: 200%;" value="m">
        </div>
    </div>

    <div class="form-group">
        <label for="edit-text" class="col-sm-2 control-label">文本</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="edit-text" style="width: 200%;" value="男">
        </div>
    </div>

    <div class="form-group">
        <label for="edit-orderNo" class="col-sm-2 control-label">排序号</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="edit-orderNo" style="width: 200%;" value="1">
        </div>
    </div>
</form>

<div style="height: 200px;"></div>
</body>
</html>

<script>
    //前端获取url网址上面的参数值
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }

    //读取字典类型数据填充select（下拉菜单）组件
    function loadType($select, valueId) {
        //清空option
        $select.empty();
        //加载type
        $.ajax({
            url: "/DictionaryType/getAll.action",
            type: "get",
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                console.log("准备开始请求：");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("请求失败：" + textStatus);
            },
            success: function (data, textStatus, jqXHR) {
                console.log("请求成功：");
                $(data).each(function (i, e) {
                    var temp = ' <option value="' + e.code + '" >' + e.name + '</option>';
                    $select.append(temp);
                });
                //开始继续修改在之前请求
                getSelectValue(valueId);
            },
            complete: function (XMLHttpRequest, textStatus) {
                console.log("响应结果了：");
            }
        });


    }

    //获取后端选中的字典数据值
    function getSelectValue(valueId) {
        //请求之前
        $.ajax({
            url: "/DictionaryValue/editBefore.action",
            type: "get",
            dataType: "json",
            data: {
                id: valueId
            },
            beforeSend: function (XMLHttpRequest) {
                console.log("准备开始请求：");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("请求失败：" + textStatus);
            },
            success: function (data, textStatus, jqXHR) {


                console.log("data.typeCode:" + data.typeCode);
                console.log($("#edit-dicTypeCode option[value='" + data.typeCode + "']"));

                $("#edit-dicTypeCode option[value='" + data.typeCode + "']").prop("selected", "selected");


                $("#edit-dicValue").val(data.value);
                $("#edit-text").val(data.text);
                $("#edit-orderNo").val(data.orderNo);
                // String id;
                // String value;
                // String text;
                // int  orderNo;
                // String typeCode;
                // DictionaryType dictionaryType;
            },
            complete: function (XMLHttpRequest, textStatus) {
                console.log("响应结果了：");
            }
        });
    }


    $(function () {




        //判定是否有id传递过来
        var urlParam = getUrlParam("id");
        if (urlParam === null) {
            console.log("id is null");
            alert("编辑的id没有提供，编辑错误");
            window.location = "index.html";
            return;
        }
        // console.log("urlParam:"+urlParam);
        //下拉框加载信息（这个方法里面嵌套了一个修改之前的ajax）
        loadType($("#edit-dicTypeCode"), urlParam);


        //更新
        $("#btnEdit").click(function () {

            //排序号的长度判定
            if ($("#edit-orderNo").val().length < 0 || $("#edit-orderNo").val().length > 7) {
                alert("排序号输入一个小于或者等于7位的整数");
                return;
            }
            //排序号的判定
            if (isNaN(parseInt($("#edit-orderNo").val()))) {
                alert("排序号必须是一个整数");
                return;
            }


            $.ajax({
                url: "/DictionaryValue/edit.action",
                type: "post",
                dataType: "json",
                data: {
                    id:urlParam,
                    value:$("#edit-dicValue").val(),
                    text:$("#edit-text").val(),
                    orderNo:$("#edit-orderNo").val(),
                    typeCode:$("#edit-dicTypeCode  option:selected").val()
                },
                beforeSend: function (XMLHttpRequest) {
                    console.log("准备开始请求：");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("请求失败：" + textStatus);
                },
                success: function (data, textStatus, jqXHR) {
                    console.log("请求成功：");
                    if (data['result'] == "success") {
                      window.location="index.html";
                    } else {
                        window.alert(data['mess']);
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    console.log("响应结果了：");
                }
            });


        });


    });
</script>